package gx.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import gx.entity.Category;
import gx.entity.Manager;
import gx.service.CategoryService;

@Controller
@RequestMapping("/mng/cate")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	// 根据ID修改
	@RequestMapping("/list.jhtml")
	public ModelAndView list() throws Exception {
		ModelAndView mav = new ModelAndView("/mng/cate/list");
		List<Category> list = categoryService.findAll();
		mav.addObject("list", list);
		return mav;
	}

	// 新增
	@RequestMapping("/add.jhtml")
	public String add(Category category) {
		categoryService.save(category);
		return "redirect:/mng/cate/list.jhtml";
	}

	// 根据ID查询详情
	@RequestMapping("/detail.jhtml")
	public ModelAndView detail(Integer id) throws Exception {
		ModelAndView mav = new ModelAndView("/mng/cate/detail");
		Category category = categoryService.get(id);
		mav.addObject("cate", category);
		return mav;
	}

	// 根据ID修改
	@RequestMapping("/edit.jhtml")
	public String edit(Integer id, String name, String summary) {
		Category category = categoryService.get(id);
		category.setName(name);
		category.setSummary(summary);
		categoryService.update(category);
		return "redirect:/mng/cate/list.jhtml";
	}

	// 删除
	@RequestMapping("/delete.jhtml")
	public String delete(Integer id) {
		categoryService.delete(id);
		System.out.println(id + "--------------------");
		return "redirect:/mng/cate/list.jhtml";
	}

	// 模糊查询
	@RequestMapping("/fruit.jhtml")
	public ModelAndView Search(String condition) throws Exception {
		ModelAndView mav = new ModelAndView("mng/cate/list");// ?
		List<Category> list = categoryService.findByName(condition);
		mav.addObject("list", list);
		return mav;
	}

}
